package com.smile.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.spring.SaBeanInject;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smile.commons.page.MyBatisPlusPageConverter;
import com.smile.commons.page.R;
import com.smile.model.dto.request.AddUserRequestDTO;
import com.smile.model.dto.request.EditUserRequestDTO;
import com.smile.model.dto.request.GetUserListRequestDTO;
import com.smile.model.response.response.GetUserByIdResponseDTO;
import com.smile.model.response.response.GetUserListResponseDTO;
import com.smile.model.response.response.GetUserSimpleInfoResponseDTO;
import com.smile.model.request.AddUserRequest;
import com.smile.model.request.EditUserRequest;
import com.smile.model.request.GetUserByIdRequest;
import com.smile.model.request.GetUserListRequest;
import com.smile.model.response.GetUserByIdResponse;
import com.smile.model.response.GetUserListResponse;
import com.smile.model.response.GetUserSimpleInfoResponse;
import com.smile.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user/system")
@Validated
public class SystemUserController {

    @Resource
    private UserService userService;
    @Autowired
    private SaBeanInject saBeanInject;

    /**
     * 重置用户密码
     *
     * @param id 用户id
     * @return
     */
    @SaIgnore
    @PostMapping("/resetPassword")
    public R<Void> resetPassword(@RequestBody(required = true) @NotNull(message = "id不能为空") Long id) {
        userService.resetPassword(id);
        return R.ok();
    }

    @PostMapping(value = "/getUserSimpleInfo", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<GetUserSimpleInfoResponse> getUserSimpleInfo() {
        GetUserSimpleInfoResponseDTO getUserSimpleInfoResponseDTO = userService.getUserSimpleInfo(Long.valueOf(StpUtil.getLoginId().toString()));
        return R.ok(BeanUtil.copyProperties(getUserSimpleInfoResponseDTO, GetUserSimpleInfoResponse.class));
    }

    /**
     * 新增用户
     *
     * @param addUserRequest
     * @return
     */
//    @SaCheckPermission("添加账号")
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Void> addUser(@RequestBody @Valid AddUserRequest addUserRequest) {
        AddUserRequestDTO addUserRequestDTO = new AddUserRequestDTO();
        BeanUtil.copyProperties(addUserRequest, addUserRequestDTO);
        Boolean result = userService.addUser(addUserRequestDTO);
        if (result) {
            return R.ok();
        } else {

            return R.fail();
        }
    }

    @PostMapping(value = "/getUserById", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<GetUserByIdResponse> getUserById(@RequestBody GetUserByIdRequest id) {
        GetUserByIdResponseDTO getUserByIdResponseDTO = userService.getUserById(id.getId());
        GetUserByIdResponse getUserByIdResponse = new GetUserByIdResponse();
        BeanUtil.copyProperties(getUserByIdResponseDTO, getUserByIdResponse);
        return R.ok(getUserByIdResponse);
    }

    @PostMapping(value = "/getUserByIds", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Map<Long, GetUserByIdResponse>> getUserByIds(@RequestBody List<Long> ids) {
        Map<Long, GetUserByIdResponseDTO> map = userService.getUserByIds(ids);
        Map<Long, GetUserByIdResponse> map1 = new HashMap<>();
        map.forEach((i1, i2) -> {
            map1.put(i1, BeanUtil.copyProperties(i2, GetUserByIdResponse.class));
        });
        return R.ok(map1);
    }

//    @SaCheckPermission("编辑账号")
    @PostMapping(value = "/edit", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Void> edit(@RequestBody @Valid EditUserRequest editUserRequest) {
        EditUserRequestDTO editUserRequestDTO = new EditUserRequestDTO();
        BeanUtil.copyProperties(editUserRequest, editUserRequestDTO);
        Boolean result = userService.edit(editUserRequestDTO);
        if (result) {
            return R.ok();
        } else {
            return R.fail();
        }
    }


    /**
     * 获取用户列表
     *
     * @return
     */
//    @SaCheckPermission("查看账号列表")
    @PostMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Page<GetUserListResponse>> getUserList(@RequestBody GetUserListRequest getUserListRequest) {

        GetUserListRequestDTO getUserListRequestDTO = new GetUserListRequestDTO();
        BeanUtil.copyProperties(getUserListRequest, getUserListRequestDTO);
        Page<GetUserListResponseDTO> userList = userService.getUserList(getUserListRequestDTO);
        return R.ok(MyBatisPlusPageConverter.convertPage(userList, GetUserListResponse.class));
    }

//    @SaCheckPermission("删除账号")
    @PostMapping(value = "/delete")
    public R<Void> deleteUser(@RequestBody Long id) {
        Boolean result = userService.deleteUser(id);
        if (result) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

}
